package com.neu.wms.security;/**
 * Author: JhonDai
 * Date: 2023/07/06/10:32
 * Version: 1.0
 * Description:
 */

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neu.wms.util.ResultVo;
import com.neu.wms.util.ServerCode;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author DAIJUNQIANG
 * @version 1.0
 * @description 配置自定义异常处理器
 * @date 2023/7/6 10:32
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
		response.setContentType("application/json;charset=UTF-8");
		String s = new ObjectMapper().writeValueAsString(ResultVo.error(ServerCode.ERR_FORBIDDEN));
		response.getWriter().println(s);
	}
}
